Conditions | 1 |
Paths | 1 |
Total Lines | 302 |
Code Lines | 170 |
Lines | 0 |
Ratio | 0 % |
Changes | 5 | ||
Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | const chai = require('chai'); |
||
116 | describe('Project', function() { |
||
117 | var testProjectId = null; |
||
118 | |||
119 | describe('Create', function() { |
||
120 | it('should create and return a project', async function() { |
||
121 | const client = await getAuthenticatedTestClient(); |
||
122 | const response = await client.createProject({ |
||
123 | title: 'New Project', |
||
124 | blocks: [], |
||
125 | }); |
||
126 | |||
127 | const responseBody = response.json; |
||
128 | |||
129 | expect(response.statusCode).to.equal(201); |
||
130 | expect(responseBody).to.matchPattern(projectSchema); |
||
131 | expect(responseBody.title).to.equal('New Project'); |
||
132 | expect(responseBody.blocks).to.be.empty; |
||
133 | |||
134 | testProjectId = responseBody._id; |
||
135 | }); |
||
136 | }); |
||
137 | |||
138 | describe('List', function() { |
||
139 | it('should list user projects paginated', async function() { |
||
140 | const client = await getAuthenticatedTestClient(); |
||
141 | const response = await client.listProjects(); |
||
142 | |||
143 | const responseBody = response.json; |
||
144 | const projects = responseBody.items; |
||
145 | |||
146 | expect(response.statusCode).to.equal(200); |
||
147 | expect(responseBody).to.matchPattern(paginatedProjectSchema); |
||
148 | projects.forEach(project => { |
||
149 | expect(project).to.matchPattern(projectSchema); |
||
150 | }); |
||
151 | }); |
||
152 | |||
153 | it('should list user projects paginated in a specific page', async function() { |
||
154 | const client = await getAuthenticatedTestClient(); |
||
155 | const response = await client.listProjects(2, 3); |
||
156 | |||
157 | const responseBody = response.json; |
||
158 | const projects = responseBody.items; |
||
159 | |||
160 | expect(response.statusCode).to.equal(200); |
||
161 | expect(responseBody).to.matchPattern(paginatedProjectSchema); |
||
162 | expect(responseBody.page).to.equal(2); |
||
163 | expect(responseBody.limit).to.equal(3); |
||
164 | projects.forEach(project => { |
||
165 | expect(project).to.matchPattern(projectSchema); |
||
166 | }); |
||
167 | }); |
||
168 | |||
169 | it('should list user projects paginated with a specific quantity of items per page', async function() { |
||
170 | const client = await getAuthenticatedTestClient(); |
||
171 | const response = await client.listProjects(2); |
||
172 | |||
173 | const responseBody = response.json; |
||
174 | const projects = responseBody.items; |
||
175 | |||
176 | expect(response.statusCode).to.equal(200); |
||
177 | expect(responseBody).to.matchPattern(paginatedProjectSchema); |
||
178 | expect(responseBody.page).to.equal(2); |
||
179 | projects.forEach(project => { |
||
180 | expect(project).to.matchPattern(projectSchema); |
||
181 | }); |
||
182 | }); |
||
183 | |||
184 | it('should list user projects paginated with a specific title search', async function() { |
||
185 | const client = await getAuthenticatedTestClient(); |
||
186 | const response = await client.listProjects(1, 6, 'Project title that does not exist'); |
||
187 | |||
188 | const responseBody = response.json; |
||
189 | const projects = responseBody.items; |
||
190 | |||
191 | expect(response.statusCode).to.equal(200); |
||
192 | expect(responseBody).to.matchPattern(`{ |
||
193 | "items": Array, |
||
194 | "totalItems": Number, |
||
195 | "page": Number, |
||
196 | "limit": Number, |
||
197 | "pages": Number, |
||
198 | "defaultCover": String, |
||
199 | }`); |
||
200 | expect(responseBody.page).to.equal(1); |
||
201 | expect(responseBody.limit).to.equal(6); |
||
202 | expect(responseBody.items.length).to.equal(0); |
||
203 | projects.forEach(project => { |
||
204 | expect(project).to.matchPattern(`{ |
||
205 | "_id": String, |
||
206 | "title": String, |
||
207 | "fonts": Array, |
||
208 | "publish": Boolean, |
||
209 | "secure": Boolean, |
||
210 | "countViews": Number, |
||
211 | "timeViews": Number, |
||
212 | "priority": Number, |
||
213 | "blocks": Array, |
||
214 | "userId": String, |
||
215 | "accountId": String, |
||
216 | "token": String, |
||
217 | "slug": String, |
||
218 | "publishURL": String, |
||
219 | "createdAt": String, |
||
220 | "updatedAt": String, |
||
221 | "__v": Number, |
||
222 | "password"?: String OR null, |
||
223 | "lastView"?: String, |
||
224 | }`); |
||
225 | }); |
||
226 | }); |
||
227 | }); |
||
228 | |||
229 | describe('Retrieve', function() { |
||
230 | it('should retrieve a existing project', async function() { |
||
231 | const client = await getAuthenticatedTestClient(); |
||
232 | const response = await client.listProject(testProjectId); |
||
233 | |||
234 | expect(response.statusCode).to.equal(200); |
||
235 | expect(response.json).to.matchPattern(projectSchema); |
||
236 | }); |
||
237 | }); |
||
238 | |||
239 | describe('Update', function() { |
||
240 | it('should update a existing project', async function() { |
||
241 | const client = await getAuthenticatedTestClient(); |
||
242 | const response = await client.updateProject(testProjectId, { |
||
243 | title: 'Updated Project Title', |
||
244 | }); |
||
245 | |||
246 | const responseBody = response.json; |
||
247 | |||
248 | expect(response.statusCode).to.equal(200); |
||
249 | expect(responseBody).to.matchPattern(projectSchema); |
||
250 | expect(responseBody.title).to.equal('Updated Project Title'); |
||
251 | }); |
||
252 | }); |
||
253 | |||
254 | describe('Clone', function() { |
||
255 | it('should clone a project and return it', async function() { |
||
256 | const client = await getAuthenticatedTestClient(); |
||
257 | const response = await client.cloneProject(testProjectId); |
||
258 | |||
259 | const responseBody = response.json; |
||
260 | |||
261 | expect(response.statusCode).to.equal(200); |
||
262 | expect(responseBody).to.matchPattern(projectSchema); |
||
263 | expect(responseBody.title).to.equal('Updated Project Title'); |
||
264 | expect(responseBody.blocks).to.be.empty; |
||
265 | }); |
||
266 | }); |
||
267 | |||
268 | describe('Password', function() { |
||
269 | it('should set project password', async function() { |
||
270 | const client = await getAuthenticatedTestClient(); |
||
271 | const response = await client.setProjectPassword(testProjectId, 'password'); |
||
272 | |||
273 | const responseBody = response.json; |
||
274 | |||
275 | expect(response.statusCode).to.equal(200); |
||
276 | expect(responseBody).to.matchPattern(projectSchema); |
||
277 | expect(responseBody.password).to.equal('password'); |
||
278 | }); |
||
279 | |||
280 | it('should check project password', async function() { |
||
281 | const client = await getUnauthenticatedTestClient(); |
||
282 | |||
283 | const correctPasswordResponse = await client.checkProjectPassword( |
||
284 | testProjectId, |
||
285 | 'password' |
||
286 | ); |
||
287 | expect(correctPasswordResponse.statusCode).to.equal(200); |
||
288 | |||
289 | const incorrectPasswordResponse = await client.checkProjectPassword( |
||
290 | testProjectId, |
||
291 | 'password1' |
||
292 | ); |
||
293 | expect(incorrectPasswordResponse.statusCode).to.equal(401); |
||
294 | }); |
||
295 | }); |
||
296 | |||
297 | describe('Publish', function() { |
||
298 | it('should set project publish to true', async function() { |
||
299 | const client = await getAuthenticatedTestClient(); |
||
300 | const response = await client.publishProject(testProjectId); |
||
301 | |||
302 | const responseBody = response.json; |
||
303 | |||
304 | expect(response.statusCode).to.equal(200); |
||
305 | expect(responseBody).to.matchPattern(projectSchema); |
||
306 | expect(responseBody.publish).to.equal(true); |
||
307 | }); |
||
308 | }); |
||
309 | |||
310 | describe('Secure', function() { |
||
311 | it('should set project secure to true', async function() { |
||
312 | const client = await getAuthenticatedTestClient(); |
||
313 | const response = await client.secureProject(testProjectId); |
||
314 | |||
315 | const responseBody = response.json; |
||
316 | |||
317 | expect(response.statusCode).to.equal(200); |
||
318 | expect(responseBody).to.matchPattern(projectSchema); |
||
319 | expect(responseBody.secure).to.equal(true); |
||
320 | }); |
||
321 | }); |
||
322 | |||
323 | describe('Cover', function() { |
||
324 | it('should generate project cover', async function() { |
||
325 | const client = await getAuthenticatedTestClient(); |
||
326 | const response = await client.generateProjectCover(testProjectId); |
||
327 | |||
328 | expect(response.statusCode).to.equal(200); |
||
329 | }); |
||
330 | }); |
||
331 | |||
332 | describe('Copy', function() { |
||
333 | it('should generate project based on a default template', async function() { |
||
334 | const templateId = '5cb47ec98497e9001ad9a1b2'; |
||
335 | const client = await getAuthenticatedTestClient(); |
||
336 | const response = await client.createProjectFromTemplate(templateId); |
||
337 | |||
338 | const responseBody = response.json; |
||
339 | |||
340 | expect(response.statusCode).to.equal(200); |
||
341 | expect(responseBody).to.matchPattern(projectSchema); |
||
342 | }); |
||
343 | }); |
||
344 | |||
345 | describe('Customer', function() { |
||
346 | it('should set a customer on the project', async function() { |
||
347 | const client = await getAuthenticatedTestClient(); |
||
348 | const response = await client.updateProject(testProjectId, { |
||
349 | customer: { |
||
350 | name: 'Gianluca Bine', |
||
351 | email: '[email protected]' |
||
352 | } |
||
353 | }); |
||
354 | |||
355 | const responseBody = response.json; |
||
356 | |||
357 | expect(response.statusCode).to.equal(200); |
||
358 | expect(responseBody).to.matchPattern(projectSchema); |
||
359 | expect(responseBody.customer.name).to.equal('Gianluca Bine'); |
||
360 | expect(responseBody.customer.email).to.equal('[email protected]'); |
||
361 | }) |
||
362 | }); |
||
363 | |||
364 | describe('Accept', function() { |
||
365 | it('should send the confirm acceptance emails', async function() { |
||
366 | const client = await getAuthenticatedTestClient(); |
||
367 | const response = await client.acceptProject(testProjectId); |
||
368 | |||
369 | expect(response.statusCode).to.equal(200); |
||
370 | }) |
||
371 | }); |
||
372 | |||
373 | describe('View and Notify', function() { |
||
374 | it('should update project last view time and send email with notification only if is not the project owner viewing', async function() { |
||
375 | const client = await getUnauthenticatedTestClient(); |
||
376 | const response = await client.viewProjectAndNotify(testProjectId); |
||
377 | |||
378 | // const responseBody = response.json; |
||
379 | |||
380 | expect(response.statusCode).to.equal(200); |
||
381 | // expect(responseBody.emailSent).to.equal(true); |
||
382 | }); |
||
383 | }); |
||
384 | |||
385 | describe('Templates', function() { |
||
386 | it('should list templates thar the user can use paginated', async function() { |
||
387 | const client = await getAuthenticatedTestClient(); |
||
388 | const response = await client.listTemplates(); |
||
389 | |||
390 | const responseBody = response.json; |
||
391 | const templates = responseBody.items; |
||
392 | |||
393 | expect(response.statusCode).to.equal(200); |
||
394 | expect(responseBody).to.matchPattern(paginatedProjectSchema); |
||
395 | templates.forEach(template => { |
||
396 | expect(template).to.matchPattern(projectSchema); |
||
397 | }); |
||
398 | }); |
||
399 | }); |
||
400 | |||
401 | describe('Remove', function() { |
||
402 | it('should delete a existing project', async function() { |
||
403 | const client = await getAuthenticatedTestClient(); |
||
404 | const newProject = await client.createProject({ |
||
405 | title: 'New Project', |
||
406 | blocks: [], |
||
407 | }); |
||
408 | |||
409 | const project = JSON.parse(newProject.body); |
||
410 | const projectId = project._id; |
||
411 | |||
412 | const response = await client.deleteProject(projectId); |
||
413 | |||
414 | expect(response.statusCode).to.equal(204); |
||
415 | }); |
||
416 | }); |
||
417 | }); |
||
418 | |||
908 |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.